import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import Home from '../components/home.vue'
import Cate from '../components/cate.vue'
import Statistic from '../components/statistic.vue'
import AddProdus from '../components/addProduct.vue'
import EditProdus from '../components/editProduct.vue'


Vue.use(VueRouter)

const routes = [
  {
    path : '/',
    redirect : '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path : '/home',
    name : 'home',
    component : Home,
    children: [
      {
        path : '/totalProduct',
        name : '总商品管理',
        component : Cate,
      },
      {
        path : '/product',
        name : '商品管理',
        component : Cate
      },
      {
        path : '/product/add',
        name : "添加商品",
        component : AddProdus
      },
      {
        path : '/product/edit',
        name : "修改商品",
        component : EditProdus
      },
      {
        path : '/dataStatistics',
        name : '数据统计',
        component : Statistic,
      },
      {
        path : '/totalDataStatistics',
        name : '总数据统计',
        component : Cate,
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next) => {
  //to 将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 函数,代表放行   ===> next()直接放行,next('强制跳转的路径')
  if(to.path == '/login') return next()
  //获取token
  const tonkenstr = window.sessionStorage.getItem("token");
  if(!tonkenstr) return next('/login')
  next()
})

export default router
